
const $common = require('../../common/common-req.js');
const router = $common.express.Router();
const requestHandler = require('../../common/request-handler.js');

/**
 * 添加项目信息
 * */
 router.post('/', (req, res) => {
     const createTime = new Date();
     const updateTime = new Date();
     const projectId = $common.strLen();
     let param = ['projectName', 'desc', 'author'];
     let vital = ['projectName', 'author'];
     let expand = {projectId, createTime, updateTime};
     let insertSql = 'insert into projects(projectId, projectName, author, `desc`, createTime, updateTime) values(?, ?, ?, ?, ?, ?)';
     let insertArr = ['projectId', 'projectName', 'author', 'desc', 'createTime', 'updateTime'];
     let moduleName = '项目信息';
     let isExitArr = ['projectName', 'author'];
     let exitTableName = 'projects';
     requestHandler.postHandler(req, res, param, vital, expand, insertSql, insertArr, moduleName, isExitArr, exitTableName);
 })
 
 /**
  * 获取用户项目列表
  * */
 router.get('/', (req, res) => {
     let param = ['pageNum', 'pageSize', 'author'];
     let vital = ['pageNum', 'pageSize', 'author'];
     let totalSql = 'select count(*) as count from projects where author=?';
     let expand = [];
     let getSql = 'select * from project_view where author=? order by updateTime limit ?, ?';
     let getArr = ['author'];
     let selectAttr = ['projectId', 'projectName', 'author', 'userName', 'desc', 'status', 'createTime', 'updateTime'];
     let reName = ['projectId', 'projectName', 'author', 'userName', 'desc', 'status', 'createTime', 'updateTime'];
     let moduleName = '用户项目列表';
     requestHandler.getHandler(req, res, param, vital, totalSql, expand, getSql, getArr, selectAttr, reName, moduleName);
 })
 
 /**
  * 修改项目信息
  * */
 router.put('/', (req, res) => {
     const updateTime = new Date();
     let param = ['projectId', 'projectName', 'desc', 'author'];
     let vital = ['projectId', 'projectName'];
     let expand = { updateTime };
     let updateSql = 'update projects set projectName=?, `desc`=?, updateTime=? where projectId=? and author=?';
     let updateArr = ['projectName', 'desc', 'updateTime', 'projectId', 'author'];
     let moduleName = '项目信息';
     requestHandler.putHandler(req, res, param, vital, expand, updateSql, updateArr, moduleName);
 })
 
 /**
  * 删除项目信息
  * */
 router.delete('/', (req, res) => {
     let param = ['projectId', 'author'];
     let vital = ['projectId', 'author'];
     let expand = [];
     let delSql = 'delete from projects where projectId = ? and author=?';
     let delArr = ['projectId', 'author'];
     let moduleName = '项目信息';
     requestHandler.delHandler(req, res, param, vital, expand, delSql, delArr, moduleName);
 })
 
module.exports = router